package com.shudai.largescreen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shudai.largescreen.pojo.User;
import com.shudai.largescreen.pojo.vo.IndustryShareVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface OperatorAnalysisMapper extends BaseMapper<User> {

    @Select({
            "SELECT count(o.id) FROM sd_order o ,sd_user_record u " +
                    "WHERE o.user_record_id = u.id " +
                    "and o.order_state = 3 and o.order_time > #{parse} "
    })
    String findAllDeal3(@Param("parse") LocalDateTime localDateTime);


    @Select({
            "<script>",

            "SELECT count(o.id)  customerNumber,o.receiver_shop_class_name typeName ",
            " FROM sd_order o ",
            "<where> 1=1  ",

            "<if test='city != null and city != \"\" '> ",
            "and o.receiver_city = #{city} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",
            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",
            "</where>",
            "GROUP BY o.receiver_shop_class_name  ",
            "ORDER BY count(o.id) desc ",

            "</script> "
    })
    List<IndustryShareVo> findAllDealNow(@Param("startMonth") LocalDateTime startMonth,
                                         @Param("endMonth") LocalDateTime endMonth,
                                         @Param("province") String province,
                                         @Param("city") String region);


    @Select({
            "SELECT o.receiver_shop_class_name typeName " +
                    " FROM sd_order o where o.receiver_shop_class_name is not null " +
                    "GROUP BY o.receiver_shop_class_name  "
    })
    List<String> findAllTypeName();


    @Select({
            "<script>",

            "SELECT count(o.id)  customerNumber ",
            " FROM sd_order o ",
            "<where> 1= 1  ",
            "<if test='region != null and region != \"\" '> ",
            "and o.receiver_city = #{region} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",
            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",
            "and  o.receiver_shop_class_name = #{type}  ",
            "</where>",
            "</script> "

    })
    Integer findTypeNow(@Param("startMonth") LocalDateTime startMonth,
                        @Param("endMonth") LocalDateTime endMonth,
                        @Param("province") String province,
                        @Param("region") String region,
                        @Param("type") String type);

    @Select({
            "<script>",
            "SELECT count(o.id)  customerNumber ",
            " FROM sd_order o ",
            "<where> 1=1 ",
            "<if test='region != null and region != \"\" '> ",
            "and o.receiver_city = #{region} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",

            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",

            " </where> ",
            "</script> "

    })
    Integer findTypeAllNow(@Param("startMonth") LocalDateTime startMonth,
                           @Param("endMonth") LocalDateTime endMonth,
                           @Param("province") String province,
                           @Param("region") String region);
}
